Longest subarray with sum <= K Problem

Longest subarray with sum <= K Problem — ExecCode Medium DSA Practice

Solve the Longest subarray with sum <= K problem on ExecCode. Free online medium DSA practice in Sliding Window. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an integer array nums (positive and negative values allowed) and an integer k, return the maximum length of a contiguous subarray whose sum equals exactly k.

Examples

Input nums = [1, -1, 5, -2, 3, 2], k = 3; Output 4. Input nums = [-2, -1, 2, 1], k = 1; Output 2. Input nums = [1, 2, 3], k = 6; Output 3

Constraints

1 ≤ nums.length ≤ 10⁵ -10⁵ ≤ nums[i] ≤ 10⁵ -10⁵ ≤ k ≤ 10⁵

Practice Longest subarray with sum <= K free on ExecCode. Browse DSA problems, topic map, and placement guides.